package longrise.phone.com.bjjt_henan.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Adapter.ServiceCenterAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.BDLocationManager;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 服务中心列表
 */

public class ServiceCenterView extends LFView implements View.OnClickListener, BDLocationListener, ILSMsgListener {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mListView;

    private ServiceCenterAdapter mAdapter;

    private List<EntityBean> datas;

    private BaseApplication mApplication;

    private BDLocationManager manager;

    private String latitude;

    private String longitude;

    private String appcaseno;

    private TextView emptyIV;//无数据时的背景色

    private TextView distanceTV;

    private TextView rateTV;

    private EntityBean[] infoBeans;

    private String isSingle;

    public ServiceCenterView(Context context, String caseno) {
        super(context);
        this.mContext = context;
        this.appcaseno = caseno;
        this.mApplication = ((MainActivity) mContext).getAppData();
    }

    public ServiceCenterView(Context context, String caseno, EntityBean[] infoBeans, String isSingle) {
        super(context);
        this.mContext = context;
        this.appcaseno = caseno;
        this.infoBeans = infoBeans;
        this.isSingle = isSingle;
        this.mApplication = ((MainActivity) mContext).getAppData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_servicecenter, null);
        if (null == mView) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("选择服务中心");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        mView.findViewById(R.id.scenter_layout_distance).setOnClickListener(this);
        mView.findViewById(R.id.scenter_layout_rate).setOnClickListener(this);
        distanceTV = (TextView) mView.findViewById(R.id.scenter_tv_distance);
        rateTV = (TextView) mView.findViewById(R.id.scenter_tv_rate);

        mListView = (PullToRefreshListView) mView.findViewById(R.id.scenter_lv);
        emptyIV = (TextView) mView.findViewById(R.id.empty_tv);
        initRefreshView();
        mAdapter = new ServiceCenterAdapter(mContext);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1) {
                    return;
                }
                if (datas == null || datas.size() < 1) {
                    return;
                }
                loadDetails(datas.get(position - 1).getString("centerid"));
            }
        });

        if (datas == null) {
            datas = new ArrayList<>();
        } else {
            datas.clear();
        }
        if (manager == null) {
            manager = new BDLocationManager(mContext, true);
            manager.setLocationListener(this);
        }

        addILSMsgListener(this);
    }

    private void initRefreshView() {
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        // 刚拉时的提示
        mListView.getLoadingLayoutProxy().setPullLabel("加载列表数据...");
        // 正在刷新提示
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");
        // 拉倒一定高度的提示
        mListView.getLoadingLayoutProxy().setReleaseLabel("放开以刷新...");
        mListView.setOnPullEventListener(new PullToRefreshBase.OnPullEventListener<ListView>() {
            @Override
            public void onPullEvent(PullToRefreshBase<ListView> refreshView, PullToRefreshBase.State state, PullToRefreshBase.Mode direction) {
//                searchCpsCenters();
            }
        });
    }

    private void loadDetails(String centerid) {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext).searchCpsCenterDetail(centerid, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    if (dataBean != null) {
                        ServiceCenterDetailView detailView = new ServiceCenterDetailView(mContext,
                                dataBean, appcaseno, infoBeans, isSingle);
                        FrameworkManager.getInstance().showNewForm(mContext, detailView);
                        return;
                    }
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(mContext, redes);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scenter_layout_distance:
                showColor(0);
                DialogUtils.showDialog(mContext);
                searchCpsCenters("0");
                break;
            case R.id.scenter_layout_rate:
                showColor(1);
                DialogUtils.showDialog(mContext);
                searchCpsCenters("1");
                break;
            default:
                break;
        }
    }

    private void showColor(int select) {
        switch (select) {
            case 0:
                distanceTV.setTextColor(mContext.getResources().getColor(R.color.themecolor));
                rateTV.setTextColor(mContext.getResources().getColor(R.color.lightblack));
                break;
            case 1:
                distanceTV.setTextColor(mContext.getResources().getColor(R.color.lightblack));
                rateTV.setTextColor(mContext.getResources().getColor(R.color.themecolor));
                break;
            default:
                break;
        }
    }

    @Override
    public void refresh() {
        DialogUtils.showDialog(mContext);
        manager.bdLocation();
    }

    @Override
    public Object onLSMsg(int i, Object... objects) {
        if (i == 24) {
            if (objects != null && objects.length > 0 && "finishservice".equals(objects[0])) {
                //关闭服务中心回到主流程
                OnDestroy();
                closeForm();
            }
        }
        return null;
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (null == mView) {
            return;
        }
        if (null != location) {
            String cityCode = location.getCityCode();
            if (!StringUtils.isEmpty(cityCode)) {
                latitude = String.valueOf(location.getLatitude());
                longitude = String.valueOf(location.getLongitude());
                // 经纬度
                searchCpsCenters("0");

                if (manager != null) {
                    manager.ondestroty();
                    manager = null;
                }
                return;
            }
        }
//        DialogUtils.closeDialog();
    }

    @Override
    public void refreshByTime() {

    }

    //查询理赔中心
    private void searchCpsCenters(String type) {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        ApiImpI.getInstance(mContext.getApplicationContext()).searchCpsCenter(null, uid, token, type, longitude, latitude,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        if (null != mListView) {// 停止刷新
                            mListView.onRefreshComplete();
                        }
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(mContext, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (null != mListView) {// 停止刷新
                            mListView.onRefreshComplete();
                        }
                        DialogUtils.closeDialog();
                        if (null == mView) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if ("1".equals(restate)) {
                            EntityBean[] beans = bean.getBeans("data");
                            if (beans != null && beans.length > 0) {
                                datas.clear();//需要进行下拉加载更多的处理
                                for (EntityBean data : beans) {
                                    datas.add(data);
                                }
                                mAdapter.setDatas(datas);
                            }
                        } else {
                            String redes = bean.getString("redes");
                            UiUtil.showToast(mContext, redes);
                        }
                        if (null == datas || datas.size() <= 0) {
                            mListView.setEmptyView(emptyIV);
                        } else {
                            emptyIV.setVisibility(View.GONE);//null
                        }
                    }
                });
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (datas != null) {
            datas.clear();
            datas = null;
        }
        if (mListView != null) {
            mListView.setAdapter(null);
            mListView = null;
        }
        if (null != manager) {
            manager.ondestroty();
            manager = null;
        }
        removeILSMsgListener(this);
        mView = null;
    }
}
